using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using DeathReportManagement.Data;
using DeathReportManagement.Models;
using DeathReportManagement.Services;

namespace DeathReportManagement.Controllers;

[Authorize(Roles = "Admin")]
public class AuditController : Controller
{
    private readonly DeathReportContext _context;
    private readonly AuthService _authService;
    
    public AuditController(DeathReportContext context, AuthService authService)
    {
        _context = context;
        _authService = authService;
    }
    
    public async Task<IActionResult> Index(int? reportId = null, DateTime? startDate = null, DateTime? endDate = null)
    {
        var query = _context.OperationLogs
            .Include(l => l.User)
            .Include(l => l.Report)
            .AsQueryable();
        
        if (reportId.HasValue)
        {
            query = query.Where(l => l.ReportId == reportId.Value);
        }
        
        if (startDate.HasValue)
        {
            query = query.Where(l => l.OperateTime >= startDate.Value);
        }
        
        if (endDate.HasValue)
        {
            query = query.Where(l => l.OperateTime <= endDate.Value.AddDays(1));
        }
        
        var logs = await query
            .OrderByDescending(l => l.OperateTime)
            .Take(1000)
            .ToListAsync();
        
        ViewBag.ReportId = reportId;
        ViewBag.StartDate = startDate?.ToString("yyyy-MM-dd");
        ViewBag.EndDate = endDate?.ToString("yyyy-MM-dd");
        
        return View(logs);
    }
    
    public async Task<IActionResult> Statistics()
    {
        var stats = new AuditStatistics();
        
        // 总报告数
        stats.TotalReports = await _context.DeathReports.CountAsync();
        
        // 待审核报告数
        stats.PendingAuditReports = await _context.DeathReports
            .CountAsync(r => r.AuditStatus == AuditStatus.Pending);
        
        // 已审核报告数
        stats.AuditedReports = await _context.DeathReports
            .CountAsync(r => r.AuditStatus == AuditStatus.Approved);
        
        // 附联已上报数
        stats.SubReportedReports = await _context.DeathReports
            .CountAsync(r => r.ReportStatus == ReportStatus.SubReported);
        
        // 主联已上报数
        stats.MainReportedReports = await _context.DeathReports
            .CountAsync(r => r.ReportStatus == ReportStatus.MainReported);
        
        // 今日创建报告数
        var today = DateTime.Today;
        stats.TodayCreatedReports = await _context.DeathReports
            .CountAsync(r => r.CreateTime >= today && r.CreateTime < today.AddDays(1));
        
        // 本月创建报告数
        var monthStart = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        stats.ThisMonthCreatedReports = await _context.DeathReports
            .CountAsync(r => r.CreateTime >= monthStart);
        
        // 近7天操作统计
        var weekAgo = DateTime.Now.AddDays(-7);
        stats.RecentOperations = await _context.OperationLogs
            .Where(l => l.OperateTime >= weekAgo)
            .GroupBy(l => l.Operation)
            .Select(g => new OperationCount
            {
                Operation = g.Key,
                Count = g.Count()
            })
            .OrderByDescending(x => x.Count)
            .ToListAsync();
        
        // 用户活跃度统计
        stats.UserActivity = await _context.OperationLogs
            .Where(l => l.OperateTime >= weekAgo)
            .GroupBy(l => l.User.Name)
            .Select(g => new UserActivity
            {
                UserName = g.Key,
                OperationCount = g.Count(),
                LastOperationTime = g.Max(x => x.OperateTime)
            })
            .OrderByDescending(x => x.OperationCount)
            .ToListAsync();
        
        return View(stats);
    }
}

public class AuditStatistics
{
    public int TotalReports { get; set; }
    public int PendingAuditReports { get; set; }
    public int AuditedReports { get; set; }
    public int SubReportedReports { get; set; }
    public int MainReportedReports { get; set; }
    public int TodayCreatedReports { get; set; }
    public int ThisMonthCreatedReports { get; set; }
    public List<OperationCount> RecentOperations { get; set; } = new();
    public List<UserActivity> UserActivity { get; set; } = new();
}

public class OperationCount
{
    public string Operation { get; set; } = string.Empty;
    public int Count { get; set; }
}

public class UserActivity
{
    public string UserName { get; set; } = string.Empty;
    public int OperationCount { get; set; }
    public DateTime LastOperationTime { get; set; }
}